const gulp = require('gulp');                        //cnpm install  --save-dev gulp
const uglify = require('gulp-uglify');          	//cnpm install  --save-dev gulp-uglify
const minify  = require('gulp-minify-css');	//cnpm install --save-dev gulp-minify-css
const babel = require('gulp-babel');     		//cnpm install --save-dev gulp-babel  需要新建 .babelrc
const replace = require('gulp-replace');    	//cnpm install  --save-dev gulp-replace
const rename = require('gulp-rename');     //cnpm install  --save-dev gulp-rename
const del = require('del');                            //cnpm install  --save-dev del
const plumber = require('gulp-plumber');  //cnpm install gulp-plumber --save-dev
const concat = require('gulp-concat');			//js合并 cnpm install --save-dev gulp-concat
//const sass = require('gulp-sass');                     //cnpm install  --save-dev gulp-sass
//const utf8Convert = require('gulp-utf8-convert');   //cnpm install gulp-utf8-convert --save-dev
//const gutil = require('gulp-util');                       //cnpm install gulp-util --save-dev
//const browserSync = require('browser-sync').create(); //npm install browser-sync gulp --save-dev

// layui
var  task = {
    //基础js
    basejs: function() {
        del("public/adminss/dist/base.min.js"); 
        return gulp.src(["public/admins/base/extend/*.js","public/admins/base/*.js"])
        .pipe(plumber())
        .pipe(babel())
        .pipe(uglify())     //压缩
        .pipe(concat('base.min.js'))  //和
        .pipe(gulp.dest( "public/admins/dist" ));
    },
    //基础css
    basecss:function() {
        del("public/admins/base/base.min.css"); 
        return gulp.src([
            "public/admins/base/style/*.css",
            "public/admins/base/controls/**/*.css"
        ])
        .pipe(plumber())
        .pipe(function(){
            return minify();
        }())
        .pipe(concat('base.min.css'))  //和并
        .pipe(gulp.dest( "public/admins/dist"  ));
    },
    //自定义控件
    controls:function() {
        var _src = ["public/admins/base/controls/**/*.js","public/admins/base/controls/*.js"];
        return gulp.src(_src)
        .pipe(plumber())
        .pipe(babel())
        .pipe(uglify())     //压缩
        .pipe(concat('controls.min.js'))  //和并
        .pipe(gulp.dest( "public/admins/dist" ));
    },
    //layui--------------------
    alljs: function(ver){
        var src = [
          './public/admins/lib/layui/**/{layui,all,'+ 'laytpl,laypage,laydate,layer,element,upload,slider,colorpicker,form,tree,transfer,table,carousel,rate,util,flow,layedit,code' +'}.js'
          ,'!./public/admins/lib/layui/**/mobile/*.js'
        ];
        return gulp.src(src).pipe(uglify())
        .pipe(concat('layui.all.js', {newLine: ''}))
        .pipe(gulp.dest('./public/admins/dist'));
    }
    //压缩css文件
    ,mincss: function(ver){
        var src = [
          './public/admins/lib/layui/css/**/*.css'
          ,'!./public/admins/lib/layui/css/**/font.css'
        ];
        //src.push('!./public/admins/lib/layui/css/layim/**.*');
        return gulp.src(src).pipe(minify({
          compatibility: 'ie7'
        }))
        .pipe(gulp.dest('./public/admins/dist/css'));
    }
    //复制iconfont文件
    ,font: function(ver){
        return gulp.src('./public/admins/lib/layui/font/*')
            .pipe(rename({}))
            .pipe(gulp.dest('./public/admins/dist/font'));
    }
    //复制组件可能所需的非css和js资源
    ,mv: function(ver){
        var src = ['./public/admins/lib/layui/**/*.{png,jpg,gif,html,mp3,json}'];
            //src.push('!./public/admins/lib/layui/**/layim/**/*.*');
            gulp.src(src).pipe(rename({}))
            .pipe(gulp.dest('./public/admins/dist'));
    }
};

//清理
gulp.task('clear', function(cb) {
  return del(['./public/admins/dist/*'], cb);
});

//完整任务 gulp 
//过滤 layim：gulp all --open、rc 版：gulp all --rc

//gulp.task('default', ['clear'], function(){ 
//	for(var key in task){
//	    task[key]();
//	}
//});

gulp.task('default',gulp.series('clear', function(done){
	for(var key in task){
	    task[key]();
	}
	done();
}));





